package com.magic.gre.ui.activity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.magic.gre.R;
import com.magic.gre.base.activity.BaseMVPActivity;
import com.magic.gre.mvp.contract.ModifyPhoneContract;
import com.magic.gre.mvp.presenter.ModifyPhonepresenterImpl;
import com.noname.lib_base_java.util.ToastUtil;
import com.noname.lib_base_java.util.ValidateUtil;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Author: Lmoumou
 * Date: 2019-01-11 11:29
 * 文件名: ModifyPhoneActivity
 * 描述:
 */
public class ModifyPhoneActivity extends BaseMVPActivity<ModifyPhonepresenterImpl> implements ModifyPhoneContract.View {

    @BindView(R.id.phone_edt)
    EditText phoneEdt;
    @BindView(R.id.code_tv)
    TextView codeTv;
    @BindView(R.id.code_edt)
    EditText codeEdt;

    public static void startThis(Context context) {
        Intent intent = new Intent(context.getApplicationContext(), ModifyPhoneActivity.class);
        context.startActivity(intent);
    }


    @Override
    protected int getLayoutId() {
        return R.layout.activity_modify_phone;
    }

    @Override
    protected ModifyPhonepresenterImpl creatPresenter() {
        return new ModifyPhonepresenterImpl(this);
    }

    @Override
    protected void netOverride() {

    }

    @Override
    protected void initView(@Nullable Bundle savedInstanceState) {
        super.initView(savedInstanceState);

    }

    @OnClick(value = {R.id.sure_modify_bt, R.id.code_tv})
    protected void onClick(View view) {
        String phoneStr = String.valueOf(phoneEdt.getText());
        switch (view.getId()) {
            case R.id.sure_modify_bt:
                String codeStr = String.valueOf(codeEdt.getText());

                if (!ValidateUtil.Mobile(phoneStr)) {
                    ToastUtil.getInstance().showNormal(this, "请输入电话号码");
                } else if (codeStr.length() != 4) {
                    ToastUtil.getInstance().showNormal(this, "请输入正确验证码");
                } else {
                    mPresenter.pChangePhone(phoneStr,codeStr);
//                    MsgEvent msgEvent=new MsgEvent(MsgEvent.MODIFY_PHONE);
//                    msgEvent.put("phone",phoneStr);
//                    RxBus.getInstance().post(msgEvent);
//                    finish();
                }
                break;
            case R.id.code_tv:
                if (!ValidateUtil.Mobile(phoneStr)){
                    ToastUtil.getInstance().showNormal(this, "请输入电话号码");
                }else {
                    mPresenter.pSmsCode(phoneStr, 0);
                }
                break;
        }
    }


    @Override
    public void vSmsCode(String code) {
        hideLoadDialog();
        codeTv.setEnabled(false);
        ToastUtil.getInstance().showNormal(this, code);
        cutdown(60 * 1000);
    }

    @Override
    public void vChangePhone() {
        finish();
    }

    private Handler handler = new Handler();

    @SuppressLint("SetTextI18n")
    private void cutdown(final int delay) {
        if (delay <= 0) {
            codeTv.setText("获取验证码");
            codeTv.setEnabled(true);
        } else {
            if (delay % 1000 == 0) {
                codeTv.setText(String.valueOf(delay / 1000) + "s");
            }
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    cutdown(delay - 100);
                }
            }, 100);
        }
    }
}
